import { Component } from '@angular/core';
import {Book} from '../../shared/model/book.interface'
import {Borrow} from "../../shared/model/borrow.interface";
import {BookApiService} from "../../core/api/book-api.service";
import {Router} from "@angular/router";
import {ConfirmationService, Message} from "primeng/api";
import {BorrowApiService} from "../../core/api/borrow-api.service";
import {UserInfoService} from "../../serve/userinfo";
import {BorrowListTemp} from "../../serve/borrowListTemp";

@Component({
  selector: 'app-book',
  templateUrl: './book.component.html',
  styleUrls: ['./book.component.css']
})
export class BookComponent {
  msgs: Message[] = [{summary: "books", severity: "info"}]
  books :Book[]=[];
  book:Book={
    bookId:'',
    bookName:''
  };
  borrow:Borrow ={
    borrowId :'',
    borrowUserId:'',
    borrowBookId:''
  };
  Once=1
  bool=true
  showView:boolean=false;
  constructor(private readonly _bookApiService: BookApiService,
              private readonly _borrowApiService: BorrowApiService,
              private readonly _router: Router,
              private readonly _confirmationService: ConfirmationService,
              private readonly _userInfo :UserInfoService,
              private readonly _AllBorrowTemp:BorrowListTemp) {
  }
  ngOnInit(): void {
    this.book.bookId='';
    this.book.bookName='';
    this._borrowApiService.getAll(this._userInfo.user.id).subscribe((borrowLists) => {
      this._AllBorrowTemp.AllborrowList=borrowLists
      console.log(this._AllBorrowTemp.AllborrowList)
    })
    this._bookApiService.count().subscribe(({count}) => {
      this.msgs = [{summary: `${count} books`, severity: "info"}];
    })
    this._bookApiService.getAll().subscribe((books)=>{
      this.books = books;
    })
  }
  creat():void {
    console.log(this.book.bookName)
    this._bookApiService.creat(this.book).subscribe()
    this.closeDialog()
    setTimeout(()=>{
      this.ngOnInit();
    },800)

  }
  deleteBook(id: number, event: any) {
    this._confirmationService.confirm({
      target: event.target,
      message: 'Are you sure that you want to delete book?',
      icon: 'pi pi-exclamation-triangle',
      accept: () => {
        //confirm action
        this._bookApiService.delete(id).subscribe({
          next: () => {
            // reload user list
            this.ngOnInit();
          },
          error: () => {
            this.msgs = [{summary: `Delete Failed!`, severity: "error"}];
          }
        })
      },
      reject: () => {
        //reject action
        console.log("master branch")
      }
    });
  }
  borrowBook(bookId:string){
    this.borrow.borrowBookId=bookId;
    this.borrow.borrowUserId=this._userInfo.user.id
    this._borrowApiService.creat(this.borrow).subscribe(()=>{
      alert("借阅成功")
    })
    this.ngOnInit()
  }

  isBorrow(bookId:string): boolean{
    if(this.Once>0){
      this._borrowApiService.getAll(this._userInfo.user.id).subscribe((borrowLists) => {
        this._AllBorrowTemp.AllborrowList=borrowLists
      })
      this.Once--
    }
    let templength = this._AllBorrowTemp.AllborrowList.length
    for (let i=0;templength>0;templength--,i++){
      if(this._AllBorrowTemp.AllborrowList[i].bookId== bookId){
        return true;
      }
    }
    return false
  }

  openDialog(){
    this.showView=true
  }
  closeDialog(){
    this.showView=false
  }
}
